#!/usr/bin/python
# -*- coding: UTF-8 -*-

import pygame
from tile import *

from pygame.locals import *

from camera import Camera
from guy import Guy
from background import *

CLOCK_TICK = 25
SECOND_TICK = 26

Game = None
class FireGame:
    size = None
    def __init__(self):
        # Initialize PyGame
        pygame.init()
        pygame.display.set_caption("The Game of Fire.")
        self.video_mode_flags = pygame.DOUBLEBUF|pygame.HWSURFACE#|pygame.FULLSCREEN#|pygame.NOFRAME

        FireGame.screen_size = (640*2, 480*2)
        
        self.screen = pygame.display.set_mode(self.screen_size, self.video_mode_flags)
        self.camera = Camera(self)
        self.background = Background("bg.png", self.screen_size)
        self.size = self.background.size
        
        maxx, maxy = self.background.size
        self.camera.set_limits(0, maxx - self.screen_size[0], 0, maxy - self.screen_size[1])

        self.framerate = 60
        self.clock = pygame.time.Clock()
        self.second_timer = pygame.time.set_timer(SECOND_TICK, 1000)
        self.framenum = 0
        self.key_mods = pygame.key.get_mods()
        self.keys = pygame.key.get_pressed()
        
        
        self.guy = Guy((0,self.size[1]/2), self)


    def move(self, time_passed):
        self.guy.move(time_passed)

    def mainLoop(self):
        done = False
        while not done:
            # Handle input events
            if self.handle_input():
                return
            self.draw()
            self.key_mods = pygame.key.get_mods()
            self.keys = pygame.key.get_pressed()
            time_passed = self.clock.tick_busy_loop(self.framerate)
            self.move(time_passed)

    def handle_input(self):
        done = False
        self.guy.handle_input()
        for event in pygame.event.get():
            if event.type == QUIT:
                done = True

            if event.type == CLOCK_TICK:
                pass
            elif event.type == SECOND_TICK:
                print(self.framenum)
                self.framenum = 0

            elif event.type == KEYDOWN:
                if event.key == K_ESCAPE:
                    done = True
                elif event.key == K_t:
                    # "This function only works under the unix x11 video driver."
                    # Go to hell, pygame, you could've made it work on Win, too!
                    #pygame.display.toggle_fullscreen()
                    self.video_mode_flags |= pygame.FULLSCREEN
                    self.screen = pygame.display.set_mode(self.screen_size, self.video_mode_flags)
        return done

    def draw(self):
        self.camera.center(self.guy.x, self.guy.y)
        self.background.draw(self.camera)
        self.guy.draw(self.camera)

        pygame.display.flip()
        self.framenum += 1


if __name__ == '__main__':
    Game = FireGame().mainLoop()
    pygame.quit()
